//
//  JKTextViewController.swift
//  JKSwiftTextView
//
//  Created by 王冲 on 2018/1/22.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//  欢迎进群交流 584599353

import UIKit

class JKTextViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 0.注册通知监听键盘弹出和消失
        NotificationCenter.default.addObserver(self, selector: #selector(JKTextViewController.keyboardChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
        
        /**
            1.控制器的基本设置
         */
        title = "JKTextView"
        view.backgroundColor = UIColor.white
        self.edgesForExtendedLayout = []
        
        /**
            2.添加UITextView和提示内容以及键盘上的view
         */
        view.addSubview(textView)
        textView.addSubview(placeholderLabel)
        /**
            3.输入框上面自定义一个view
         */
        view.addSubview(toolBar)
        toolBar.addSubview(toolBarBtn)
        
    }
    
    /**
     只要键盘改变就会调用
     */
    @objc func keyboardChange(notify: NSNotification)
    {
        // 1.取出键盘最终的rect
        let value = notify.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
        let rect = value.cgRectValue
        
        // 2.修改工具条的约束
        // 弹出 : Y = 409 height = 258
        // 关闭 : Y = 667 height = 258
        // 667 - 409 = 258
        // 667 - 667 = 0
        
        let duration = notify.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
        /*
         工具条回弹是因为执行了两次动画, 而系统自带的键盘的动画节奏(曲线) 7
         7在apple API中并没有提供给我们, 但是我们可以使用
         7这种节奏有一个特点: 如果连续执行两次动画, 不管上一次有没有执行完毕, 都会立刻执行下一次
         也就是说上一次可能会被忽略
         
         如果将动画节奏设置为7, 那么动画的时长无论如何都会自动修改为0.5
         
         UIView动画的本质是核心动画, 所以可以给核心动画设置动画节奏
         */
        // 1.取出键盘的动画节奏
        let curve = notify.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
        
        UIView.animate(withDuration: duration.doubleValue) { () -> Void in
            
            // 2.设置动画节奏
            UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: curve.intValue)!)
            self.toolBar.frame.origin.y = rect.origin.y - 64 - 44
        }
        
        let anim = toolBar.layer.animation(forKey: "position")
        print("duration = \(String(describing: anim?.duration))")
    }
    
    // MARK: 切换键盘
    @objc func switchKeyBoard() {
        
        print("切换表情键盘")
        print(#function)
        // 结论: 如果是系统自带的键盘, 那么inputView = nil
        //      如果不是系统自带的键盘, 那么inputView != nil
        //        print(textView.inputView)
        
        // 1.关闭键盘
        textView.resignFirstResponder()
        
        // 2.设置inputView
        textView.inputView = (textView.inputView == nil) ? keyBoardView : nil
        
        // 3.从新召唤出键盘
        textView.becomeFirstResponder()
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: UITextView 的创建
    private lazy var textView:UITextView = {
        
        let textview = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
        textview.font = UIFont.systemFont(ofSize: 20)
        // 垂直水平方向有弹性
        textview.alwaysBounceVertical = true
        // 垂直方向滑动键盘下去
        textview.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
        return textview
    }()
  
    // MARK: UILabel 提示文本
    private lazy var placeholderLabel: UILabel = {
        
        let label = UILabel(frame: CGRect(x: 5, y: 9, width: UIScreen.main.bounds.width-10, height: 22))
        label.font = UIFont .systemFont(ofSize: 20)
        label.textColor = UIColor.red
        label.text = "分享新鲜事"
        return label
        
    }()
    
    // MARK: UIView 键盘上加个view
    private lazy var toolBar: UIView = {
        
        let inputView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height-64-44, width: UIScreen.main.bounds.width, height: 44))
        inputView.backgroundColor = UIColor.green
        return inputView
        
    }()
    
    // MARK: UIView 键盘上加个view 上面加个button
    private lazy var toolBarBtn: UIButton = {
        
        let btn = UIButton(frame: CGRect(x: UIScreen.main.bounds.width-150, y: 0, width: 100, height: 44))
        btn.backgroundColor = UIColor.purple
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitle("切换键盘", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(JKTextViewController.switchKeyBoard), for: UIControlEvents.touchUpInside)
        return btn
        
    }()
    
    // MARK: 自定义键盘
    private lazy var keyBoardView: UIView = {
        
        let keyboardView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
        keyboardView.backgroundColor = UIColor.brown
        return keyboardView
        
    }()
}

// MARK: UITextView代理的实现
extension JKTextViewController: UITextViewDelegate{
    
    func textViewDidChange(_ textView: UITextView) {
        
        /**
            hasText 可以判断UITextView是否有内容
         */
        placeholderLabel.isHidden = textView.hasText

    }
    
}
